﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;
using System.Collections;

namespace UI
{
    public class UIUnionFightCombatLog : MonoBehaviour
    {

        public UIUnionFightRunMap page;
        public GameObject template;
        public SimpleGrid grid;
        public UILabel selfName;
        public UILabel enemyName;
        public UILabel selfScore;
        public UILabel enemyScore;
        public UIScrollView scrollView;

        List<GameObject> gos = new List<GameObject>();
        bool isShowMovieBtn;
        void Awake()
        {
            template.SetActive(false);
        }

        public void Init()
        {
            UIUnionFightRunMap.GetNetData().SendCSUFFightRecord();
            gameObject.SetActive(true);
            GetComponent<PageTweener>().Play();
        }


        public void Exit()
        {
            foreach (var obj in gos)
            {
                GameObject.Destroy(obj);
            }
            gos.Clear();
            gameObject.SetActive(false);
        }


        public void InitView(SCUFFightRecord msg)
        {
            List<UFFightRecord> recordList = msg.records;
            List<UFFightUnionRecord> unionList = msg.unionInfos;

            var netBattle = UIUnionFightRunMap.GetNetData().netBattle;
            var selfUnion = netBattle.GetUnoin(true);
            var enemyUnion = netBattle.GetUnoin(false);
            for (int i = 0;i < unionList.Count; i++)
            {
                if(selfUnion.unionId == unionList[i].unionId)
                {
                    selfName.text = string.Format(TableTextBlo.GetText("union_fight_name"), selfUnion.serverId, selfUnion.unionName);
                    selfScore.text = unionList[i].killNum + "";
                }
                else if(enemyUnion.unionId == unionList[i].unionId)
                {
                    enemyName.text = string.Format(TableTextBlo.GetText("union_fight_name"), enemyUnion.serverId, enemyUnion.unionName);
                    enemyScore.text = unionList[i].killNum + "";
                }
            }
            recordList.Sort(delegate (UFFightRecord a, UFFightRecord b)
            {
                if (a.time > b.time)
                    return 1;
                else if (a.time < b.time)
                    return -1;
                return 0;
            });
            isShowMovieBtn = CommonMethod.CompareVersion(msg.appVersion, msg.resVersion, GameConfig.Instance.AppVersion, GameConfig.Instance.ResVersion);

            StartCoroutine(_InitView(recordList));
        }

        IEnumerator _InitView(List<UFFightRecord> list)
        {
            foreach (var obj in gos)
            {
                GameObject.Destroy(obj);
            }
            gos.Clear();
            foreach (var item in grid.childs)
                Destroy(item);
            grid.childs.Clear();
            yield return null;

            int i = 0;
            foreach (var mv in list)
            {
                var inst = GameObject.Instantiate(template);
                grid.AddChild(inst);
                inst.SetActive(true);
                inst.GetComponent<UIunionFightLogItem>().SetItem(mv, isShowMovieBtn);
                gos.Add(inst);
                if (++i % 4 == 0)
                    yield return null;
                grid.Reposition();
            }
            scrollView.enabled = list.Count > 4;
        }
    }
}

